<template>
  <div class="role">
    <el-card shadow="never">
      <el-form label-width="70px" :inline="true">
        <el-form-item label="网络">
          <el-input v-model="paramFrom.confirmNetwork" size="mini" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="mini" @click="query">查询</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="primary" size="mini" @click="addCoupon">新增加密货币</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" style="margin-top: 20px;">
      <!--      <el-row type="flex" justify="end" style="margin-bottom: 15px;">-->
      <!--        <el-button type="primary" size="mini" @click="btnOK">添加员工</el-button>-->
      <!--      </el-row>-->
      <el-table border :data="tableData" style="width: 100%">

        <el-table-column prop="confirmCrypto" align="center" label="加密货币" width="100px">
        </el-table-column>
        <el-table-column prop="confirmNetwork" align="center" label="网络" width="200px" />
        <el-table-column prop="confirmAddress" align="center" label="地址" width="250px">
        </el-table-column>
        <el-table-column prop="confirmCode" align="center" label="收款码">
          <template slot-scope="scope">
            <img style="width: 30px;height: 30px;" :src="'data:image/png/jpg;base64,' + scope.row.confirmCode" alt="">
          </template>
        </el-table-column>
        <el-table-column prop="creationTime" align="center" label="创建时间" width="200px" />
        <el-table-column align="center" label="操作" width="200px">
          <template v-slot="{ row }">
            <el-button type="text" @click="patchBtn(row)">修改</el-button>
            <el-button type="text" @click="delBtn(row)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <paging :page-size="paramFrom.pageSize" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />

      <!-- 删除的对话框 -->
      <el-dialog title="删除" :visible="showVisible" width="30%" :before-close="handleClose">
        <div style="text-align: center;line-height: 30px">
          <span style="font-size: 16px;">你确定要删除这个加密货币吗？</span>
        </div>

        <el-row type="flex" justify="center" style="margin-top: 20px;">
          <el-button size="mini" @click="handleClose">取 消</el-button>
          <el-button size="mini" type="primary" @click="delBtnOK">确 定</el-button>
        </el-row>
      </el-dialog>
      <!-- 新增对话框 -->
      <el-dialog title="新增" :visible.sync="dialogFormVisibleCoupon" width="35%">
        <el-form :model="ruleForm" label-width="30%" ref="ruleForm" :rules="rules">

          <el-form-item label="Crypto" label-width="80px" prop="confirmCrypto">
            <el-select v-model="ruleForm.confirmCrypto" placeholder="请选择" size="mini">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="Network" label-width="80px" prop="confirmNetwork">
            <el-input v-model="ruleForm.confirmNetwork" autocomplete="off" size="mini"></el-input>
          </el-form-item>
          <el-form-item label="地址" label-width="80px" prop="confirmAddress">
            <el-input v-model="ruleForm.confirmAddress" autocomplete="off" class="topbut botbut" size="mini"></el-input>
          </el-form-item>
          <el-form-item label="上传照片" label-width="80px" prop="confirmCode">
            <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleSuccess" :on-error="handleError"
              :auto-upload="true" :file-list="fileList" drag :headers="customHeaders" v-model="ruleForm.confirmCode"
              :limit="1">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleCoupon = false" size="mini">取 消</el-button>
          <el-button type="primary" @click="submitAddCoupon" size="mini">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 修改对话框 -->
      <el-dialog title="修改" :visible.sync="dialogFormVisibleCouponUpdate" width="35%">
        <el-form :model="ruleForm" label-width="30%" ref="ruleForm">

          <el-form-item label="Crypto" label-width="80px">
            <el-select v-model="ruleForm.confirmCrypto" placeholder="请选择" size="mini">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="Network" label-width="80px">
            <el-input v-model="ruleForm.confirmNetwork" autocomplete="off" size="mini"></el-input>
          </el-form-item>
          <el-form-item label="地址" label-width="80px">
            <el-input v-model="ruleForm.confirmAddress" autocomplete="off" class="topbut botbut" size="mini"></el-input>
          </el-form-item>
          <el-form-item label="重新上传" label-width="80px">
            <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleSuccess" :on-error="handleError"
              :auto-upload="true" :file-list="fileList" drag :headers="customHeaders" v-model="ruleForm.confirmCode"
              :limit="1">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip" slot="tip">支持任意格式文件上传。</div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="quxiao">取 消</el-button>
          <el-button type="primary" @click="submitUpdateCoupon">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
// import { deleteCoupon, getCouponList, randomCoupon, submitAddCoupon } from '@/api/staff'
import { getBase64, addCrypto, getCrypto, delCrypto, editCrypto } from '@/api/Profit'
import paging from '@/components/paging'
import { TOKEN_KEY } from "@/constants/KEY";
import Cookies from "js-cookie";
import baseData from '@/baseData'

export default {
  name: 'VueAdminIndex',
  components: {
    paging,
  },
  data() {
    return {
      customHeaders: {
        Authentication: Cookies.get(TOKEN_KEY)
      },
      header: {
        Authentication: Cookies.get(TOKEN_KEY)
      },
      dialogFormVisibleCouponUpdate: false, //修改弹窗
      dialogFormVisibleCoupon: false, // 新增优惠卷弹出框
      paramFrom: {
        confirmNetwork: null,
        page: 1,
        pageSize: 10,

      },
      showVisible: false,
      tableData: [
      ],
      ruleForm: {
        confirmAddress: null,
        confirmCode: null,
        confirmCrypto: null,
        confirmNetwork: null
      },
      status: 0,
      total: 0,
      // 参数
      dialogVisible: false,
      currid: null,
      couponCode: null,
      rowid: null,
      statusList: [
        { id: null, value: '全部' },
        { id: 0, value: '未使用' },
        { id: 1, value: '已下发' },
        { id: 2, value: '已使用' },
        { id: 3, value: '已过期' },
      ],
      fileList: [],

      uploadUrl: baseData.basePathAdmin+'/cryptocurrency/pingTbConfirmCode',
      paymentResult: '',
      options: [
        { id: 1, name: 'BTC' },
        { id: 2, name: 'USDT' },
        { id: 3, name: 'USDC' },
        { id: 4, name: 'ETH' },
        { id: 5, name: 'LTC' },
      ],
      rules: {
        confirmCrypto: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        confirmNetwork: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        confirmAddress: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        confirmCode: [
          { required: true, message: '请上传', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {
    // this.getCouponList()
    this.getCryptoList()
  },

  methods: {
    // 获取数据
    async getCryptoList() {
      const res = await getCrypto(this.paramFrom)
      console.log(res, 'abc')
      this.tableData = res.records
      this.tableData.forEach((item) => {
        console.log(item)
        item.imgName = ''
      }
      )
    },

    // 后期要改
    handleSizeChange(val) {
      this.paramFrom.pageSize = val
      // this.getCouponList()
    },
    handleCurrentChange(val) {
      this.paramFrom.page = val
      // this.getCouponList()
    },
    // 删除
    delBtn(row) {
      // this.staffid = row.id
      // this.couponCode = row.couponCode
      this.showVisible = true
      console.log(row.id)
      this.rowid = row.id
    },
    // 取消
    handleClose() {
      this.showVisible = false
    },
    delBtnOK() {
      console.log(this.rowid, 'id')
      const res = delCrypto(this.rowid)
      this.$message({
        message: "删除加密货币成功!",
        type: 'success'
      })
      this.getCryptoList()
      this.showVisible = false
      this.getCryptoList()
    },

    // 新增
    addCoupon() {
      this.ruleForm.value = null
      this.dialogFormVisibleCoupon = true
      this.randomCoupon()
      this.ruleForm.confirmCode = ''
      this.ruleForm.confirmAddress = ''
      this.ruleForm.confirmNetwork = ''
      this.ruleForm.confirmCrypto = ''
      this.fileList = []
    },

    // 添加确定
    async submitAddCoupon() {
      await this.$refs.ruleForm.validate()
      const res = await addCrypto(this.ruleForm)
      this.dialogFormVisibleCoupon = false
      this.getCryptoList()
      this.dialogFormVisibleCoupon = false
      this.$message({
        message: res.paymentResult,
        type: 'success'
      })
      this.dialogFormVisibleCoupon = false
    },


    async randomCoupon() {
      const res = await randomCoupon()
      this.ruleForm.couponCode = res.paymentResult
    },


    // 搜索
    query() {
      this.getCryptoList()
    },

    // 上传图片
    handleSuccess(response, file, fileList) {
      console.log(response, fileList, 'cc');
      this.fileList = fileList
      this.ruleForm.confirmCode = response.data.paymentResult
      // 确保fileList只保留最新的一个文件
      // this.fileList = fileList.slice(-1);
      // console.log(this.paymentResult)
      // this.fileList = fileList
      // console.log(this.fileList)
      // const res = getBase64({
      //   file: file
      // })
      // console.log(res)
      // console.log(this.faceImage);
    },


    // 修改
    patchBtn(row) {
      row.imgName = "加密货币二维码"
      this.dialogFormVisibleCouponUpdate = true
      console.log(row, this.img)
      this.ruleForm.confirmCrypto = row.confirmCrypto
      this.ruleForm.confirmNetwork = row.confirmNetwork
      this.ruleForm.confirmAddress = row.confirmAddress
      this.ruleForm.confirmCode = row.confirmCode
      let obj = [{
        url: 'data:image/jpeg;base64,' + row.confirmCode,
        name: row.imgName
      }]
      this.fileList = obj
      this.rowid = row.id
    },
    // 确定修改
    submitUpdateCoupon() {
      const res = editCrypto({
        confirmAddress: this.ruleForm.confirmAddress,
        confirmCode: this.ruleForm.confirmCode,
        confirmCrypto: this.ruleForm.confirmCrypto,
        confirmNetwork: this.ruleForm.confirmNetwork,
        id: this.rowid
      })
      this.getCryptoList()
      this.dialogFormVisibleCouponUpdate = false
      this.$message({
        message: '修改成功',
        type: 'success'
      })
      this.getCryptoList()
      console.log(res)
      this.$refs.ruleForm.reset()
      this.$refs.ruleForm.resetFields()
    },
    // 取消
    quxiao() {
      this.dialogFormVisibleCouponUpdate = false
      this.$refs.ruleForm.resetFields()
      this.fileList = []
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .topbut input::-webkit-outer-spin-button,
::v-deep .topbut input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

::v-deep .topbut input[type="number"] {
  -moz-appearance: textfield;
}

::v-deep .botbut inpit {
  border: none
}

@media (max-width:768px) {

  .el-form-item {
    display: flex;


  }
}
</style>
